window.jptShare = (function(){
	var instantce;
	return {
		getInstance:function(){
			return instantce || (instantce = JptShareTool());
		}
	};

	function JptShareTool(){
		return {
			_def:function(){
				this.shareqqUrl = "http://connect.qq.com/widget/shareqq/index.html?";
				this.shareqqzoneUrl = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
				this.shareqqWeibo = "http://v.t.qq.com/share/share.php?";
				this.sharekaixin = "http://www.kaixin001.com/~repaste/repaste.php?";
				this.sharerenren = "http://widget.renren.com/dialog/share?";
				this.sharesina = "http://service.weibo.com/share/share.php?";
				this.commands = [];
				this.commands.push({comd:"qq",doing:this.qq});
				this.commands.push({comd:"qqzone",doing:this.qqzone});
				this.commands.push({comd:"qqweibo",doing:this.qqweibo});
				this.commands.push({comd:"weixin",doing:this.weixin});
				this.commands.push({comd:"kaixin",doing:this.kaixin});
				this.commands.push({comd:"sinaweibo",doing:this.sinaweibo});
				this.commands.push({comd:"renren",doing:this.renren});
				this.commands.push({comd:"inviteFriends",doing:this.inviteFriends});
				this.commands.push({comd:"invitewxFriends",doing:this.invitewxFriends});
			},
			option:function(opt){
				this.shareOpt = $.extend({},{
					orgUrl:"https://www.jplicai.com",    //备用URL
					pics:"",                                 //图片
					targetUrl:"https://www.jplicai.com", //分享目标链接
					title:"金票理财",   //标题
					summary:"金票理财", //摘要，qq|qqzone 时有效
					desc:"金票理财",    //内容
					params:{},        
					resMode:"url"   //共有两种 resMode : url(返回解析好的url由使用者自己调用),window:根据参数直接打开窗口
				},opt);
			},
			docommand:function(commandNm)
			{
				var _this = this;
				if(_this.shareOpt){
					_this._def();
					var execCommand =  $.grep(_this.commands,function(cmd){
						if(cmd.comd === commandNm){
							return true;
						}
					});
					if(execCommand && execCommand.length == 1)
					{
						var execRes = execCommand[0].doing.call(_this);
						if(_this.shareOpt.resMode=="url"){
							return execRes;
						}else{
							if(commandNm == "weixin"){
								_this.createWx2dPic(execRes);
							}else if(commandNm == "inviteFriends"){
								_this.createinvist(execRes);
							}else if(commandNm == "invitewxFriends"){
								_this.createInvisteWx2dPic(execRes);
							}else{
								window.open(execRes);	
							}
						}
					}
				}
			},
			qq:function(){
				return this.qqhytoqqzone("qq");
			},
			qqzone:function(){
				return this.qqhytoqqzone("qqzone");
			},
			qqweibo:function(){
				var p = {};
				p.c = "share";
				p.a = "index";
				p.url = this.shareOpt.targetUrl;
				p.title = this.shareOpt.desc;
				p.appkey = encodeURI("appkey");
				p.pic = encodeURI(this.shareOpt.pics);
				// this.sharePicSetting(p,"pics",this.shareOpt.pics);
				p.site = 'jinpiaotong';
				var urlStr = this.shareqqWeibo + this.geturlEncodeComponent(p);
				return urlStr;
			},
			qqhytoqqzone:function(type){
				var p = {};
				p.url = this.shareOpt.targetUrl;
				p.title = this.shareOpt.title;
				p.desc = this.shareOpt.desc;
				p.summary = this.shareOpt.summary;
				p.site = 'jinpiaotong';
				this.sharePicSetting(p,"pics",this.shareOpt.pics);
				var urlStr = this.geturlEncodeComponent(p);
				if(type=="qq"){
					return this.shareqqUrl + urlStr; 
				}else{
					return this.shareqqzoneUrl + urlStr; 
				}
			},
			weixin:function(){
				var p = {};
				p.site = this.shareOpt.targetUrl;
				var urlStr = p.site + this.geturlEncodeComponent(this.shareOpt.params);
				return urlStr;
			},
			invitewxFriends:function(){
				var p = {};
				p.site = this.shareOpt.targetUrl;
				var urlStr = p.site + this.geturlEncodeComponent(this.shareOpt.params);
				return urlStr;
			},
			inviteFriends:function(){
				var p = {};
				p.site = this.shareOpt.targetUrl;
				var urlStr = p.site;
				return urlStr;
			},
			kaixin:function(){
				var p = {};
				p.rurl = this.shareOpt.targetUrl;
				p.rtitle = this.shareOpt.title;
				p.rcontent = this.shareOpt.desc;
				var urlStr = this.sharekaixin + this.geturlEncodeComponent(p);
				return urlStr;
			},
			sinaweibo:function(){
				var p = {};
				this.sharePicSetting(p,"pic",this.shareOpt.pics);
				p.title = this.shareOpt.desc;
				p.url = this.shareOpt.targetUrl;
				var urlStr = this.sharesina + this.geturlEncodeComponent(p);
				return urlStr;
			},
			renren:function(){
				var p = {};
				p.resourceUrl = this.shareOpt.targetUrl;
				p.srcUrl = this.shareOpt.orgUrl;
				p.title = this.shareOpt.title;
				p.description = this.shareOpt.desc;
				var urlStr = this.sharerenren + this.geturlEncodeComponent(p);
				return urlStr;
			},
			sharePicSetting:function(object,key,pics){
				if(pics!=""){
					object[key] = pics;
				}
			},
			geturlEncodeComponent:function(p){
				var s = [];
				for(var i in p){
					s.push(i + '=' + encodeURIComponent(p[i]||''));
				}
				if(s.length>0){
					return s.join("&");
				}else{
					return "";
				}
			},
			createWx2dPic:function(url){
				var $this = this;
				if(!url){
					return;
				}
				$this.append2dTool();
				function draw2dPic() {
					$("#wx2dpic").empty();
					try {
						if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
							$("#wx2dpic").qrcode({
								render: "table",
								width: 120,
								height: 120,
								text: url
							});
						} else {
							$("#wx2dpic").qrcode({
								width: 120,
								height: 120,
								text: url
							});
						}
					}catch(Exception){
						$("#wx2dpic").qrcode({text:url});
					}
				}
				setTimeout(function(){
					draw2dPic();
				},200);
			},
			createInvisteWx2dPic:function(url){
				var $this = this;
				$this.append2dTool();
				function draw2dPic() {
					$("#wx2dpic").empty();
					try {
						if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
							$("#wx2dpic").qrcode({
								render: "table",
								width: 170,
								height: 170,
								text: url
							});
						} else {
							$("#wx2dpic").qrcode({
								width: 170,
								height: 170,
								text: url
							});
						}
					}catch(Exception){
						$("#wx2dpic").qrcode({text:url});
					}
				}
				setTimeout(function(){
					draw2dPic();
				},200);
			},
			createinvist:function(url){
				var $this = this;
				$this.append2dTool();
				function drawinvist() {
					$("#third-ewm").empty();
					try {
						if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)) {
							$("#third-ewm").qrcode({
								render: "table",
								width: 115,
								height: 115,
								text: url
							});
						} else {
							$("#third-ewm").qrcode({
								width: 115,
								height: 115,
								text: url
							});
						}
					}catch(Exception){
						$("#third-ewm").qrcode({text:url});
					}
				}
				setTimeout(function(){
					drawinvist();
				},200);
			},
			append2dTool:function(){
				var fileref = document.createElement('script');
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src","jquery.qrcode.min.js");
			}
		};
	}
})();

function shareDialog(command,opt){
	window.jptShare.getInstance().option(opt);
	jptShare.getInstance().docommand(command);
}
